package net.itst.schwarzerdaumen;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class AddPlant extends Activity {

    private static final String TAG = "XXX";
    private Button buttonShoot;
    private Button buttonSubmit;
    private Uri selectedImage;
    private long selectedImageId;
    private SQLiteDatabase db;
    private SchwarzerDaumenDatabase database;
    private Toast tNoNameError = null;

    private static final int ACT_SHOOT_PHOTO = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	database = new SchwarzerDaumenDatabase(this);
	db = database.getReadableDatabase();

	setTitle(R.string.plantmenu_addPlant);
	setContentView(R.layout.add_plant);

	buttonShoot = (Button) findViewById(R.id.btn_add_plant_shoot);
	buttonShoot.setOnClickListener(new OnClickListener() {
	    public void onClick(View v) {
		chooseImage();
	    }
	});

	buttonSubmit = (Button) findViewById(R.id.btn_add_plant_submit);
	buttonSubmit.setOnClickListener(new OnClickListener() {
	    public void onClick(View v) {
		savePlant();
	    }
	});
    }

    @Override
    public void onStart() {
	super.onStart();
    }

    @Override
    public void onResume() {
	super.onResume();
    }

    @Override
    public void onDestroy() {
	super.onDestroy();
	db.close();
    }

    private void chooseImage() {
	Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
	startActivityForResult(i, ACT_SHOOT_PHOTO);
	Log.d(TAG, "Intent issued: " + MediaStore.ACTION_IMAGE_CAPTURE);
    }

    private void displayImage(Intent image) {
	ImageView iv;
	iv = (ImageView) findViewById(R.id.add_plant_thumbnail);
	iv.setImageBitmap((Bitmap) image.getExtras().get("data"));
    }

    private void savePlant() {
	EditText plantName = (EditText) findViewById(R.id.add_plant_name);
	ContentValues values = new ContentValues();

	if (plantName.getText().toString().equals("")) {
	    if (tNoNameError == null) {
		tNoNameError = Toast.makeText(AddPlant.this,
			R.string._err_NoNameGiven, Toast.LENGTH_LONG);
	    }
	    tNoNameError.show();
	} else {
	    values.put(TablePlants.NAME, plantName.getText().toString());

	    try {
		values.put(TablePlants.IMAGE, selectedImage.toString());
	    } catch (Exception e) {
	    }

	    long rowIdPlant = db.insert(TablePlants.TABLE_NAME, null, values);

	    Intent intent = new Intent();
	    setResult(RESULT_OK, intent);
	    finish();
	}
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	Log.d(TAG, "Activity resulted in: requestCode = "
		+ String.valueOf(requestCode) + " - resultCode = "
		+ String.valueOf(resultCode));
	if (requestCode == ACT_SHOOT_PHOTO) {
	    if (resultCode == Activity.RESULT_OK) {
		selectedImage = data.getData();
		Bitmap bm = (Bitmap) data.getExtras().get("data");
		MediaStore.Images.Media.insertImage(getContentResolver(), bm,
			null, null);

		String[] fields = { MediaStore.Images.Media._ID };
		Cursor getImageInfo = this.managedQuery(selectedImage, fields,
			null, null, null);
		selectedImageId = getImageInfo
			.getColumnIndex(MediaStore.Images.Media._ID);

		getImageInfo.close();

		Log.d(TAG, "onPicked: " + selectedImage.toString());
		displayImage(data);
	    }
	}
    }
}
